
var stockList = new Vue({
    el: '#stockList',
    data: {
        message: 'Hello Vue!',
        list: [],
        sort: '',
        curPage: 1,
        param: {},
        page: {
            begin: 1,
            end: 5
        }
    },
    created: function () {
        let that = this;
        let search = location.search.slice(1);
        // console.log(search.split('&'));
        location.search.slice(1).split('&').forEach(function (val) {
            let list = val.split('=');
            that.param[list[0]] = list[1];
        });
        // console.log(this.param);
        this.getData(this.param);
    },
    methods: {
        refresh: function () {
            // console.log('get data');
            // console.log(location.host + "/stock/getHistory");

            this.getData({});
        },
        order: function (event) {
            let that = this;
            // console.log(event.target.innerText);
            // console.log($(event.target).data('sort'));
            let sort = $(event.target).data('sort');
            this.sort = this.sort ? '' : sort;
            this.list.sort(function (a, b) {
                let order = that.sort ? a[sort] > b[sort] : a[sort] < b[sort];
                return order;
            });
        },
        getPage: function (event) {
            let that = this;
            let page = event.target.innerText;
            // console.log(page);
            if (isNaN(parseInt(page))) {
                
            }
            else {
                this.getData({ page });
            }
        },
        getData: function (option) {
            option = option || {};
            let code = option.code || this.param.code,
                record = option.record || this.param.record,
                page = option.page || 1,
                order = option.order || this.param.order;
            
            let queryString = $.param({ code, record, order, page });

            // console.log(location.origin + '/data/stockHistory?' + queryString);
            
            $.get(location.origin + '/data/stockHistory?' + queryString,
                function (data) {
                    // console.log(data);
                    stockList.list = [];
                    for (let i = 0; i < data.length; i++) {
                        let stock = data[i];
                        let list = [stock.code, stock.id, stock.date, stock.close];
                        stockList.list.push(list);
                    }
                }, "json");
        }
    }
});

